package com.example.city_defense;

import java.util.ArrayList;
import java.util.List;

import android.R.color;
import android.graphics.Point;
import android.util.Log;
import android.view.animation.BounceInterpolator;

import com.badlogic.androidgames.framework.Game;
import com.badlogic.androidgames.framework.Graphics;
import com.badlogic.androidgames.framework.Graphics.PixmapFormat;
import com.badlogic.androidgames.framework.Input.TouchEvent;
import com.badlogic.androidgames.framework.Screen;

public class GameScreen extends Screen {

	enum GameState {
		Running, Paused, GameOver
	}

	enum GameActivity {
		Sell, Build, NoAction
	};

	enum BarState {
		Basic, Advanced, Abilities
	};

	GameState state = GameState.Running;
	GameActivity activity = GameActivity.NoAction;
	BarState barState = BarState.Basic;

	ArrayList<Point> listTower;
	World world;

	int level;
	int map;
	int Money = 350;
	int item = 0;

	public GameScreen(Game game, int level, int map) {
		super(game);
		// TODO Auto-generated constructor stub
		world = new World(map, level);

		this.map = map;
		this.level = level;
		this.listTower = new ArrayList<Point>();
		this.loadingMap();
	}

	private void loadingMap() {
		String name = "map" + map + ".png";
		Asset.map = game.getGraphics().newPixmap(name, PixmapFormat.ARGB4444);

	}

	@Override
	public void update(float deltaTime) {
		// TODO Auto-generated method stub
		List<TouchEvent> touchEvents = game.getInput().getTouchEvents();

		if (state == GameState.Running) {
			updateRunning(touchEvents);
		}
		if (state == GameState.Paused) {
			updatePause(touchEvents);
		}
		if (state == GameState.GameOver) {
			updateGameOver(touchEvents);
		}

	}

	public void updateRunning(List<TouchEvent> touchEvents) {
		int len = touchEvents.size();

		for (int i = 0; i < len; i++) {
			TouchEvent touchEvent = touchEvents.get(i);
			// Check Exit
			if (inBounds(touchEvent, 750, 1250, 50, 50)
					&& touchEvent.type == TouchEvent.TOUCH_DOWN) {
				System.exit(0);
			}
			// Only check Touch Down
			if (touchEvent.type == TouchEvent.TOUCH_DOWN) {

				// Touch Bar and Items Menu
				if (touchEvent.y >= 1030) {
					// Basic Bar
					if (inBounds(touchEvent, 0, 1030, 160, 50)) {
						this.barState = BarState.Basic;
						this.activity = GameActivity.NoAction;
						this.item = 0;
						Asset.click.play(1);
					}
					// Advanced Bar
					else if (inBounds(touchEvent, 160, 1030, 160, 50)) {
						this.barState = BarState.Advanced;
						this.activity = GameActivity.NoAction;
						this.item = 0;
						Asset.click.play(1);
					}
					// Abilities Bar
					else if (inBounds(touchEvent, 320, 1030, 160, 50)) {
						this.barState = BarState.Abilities;
						this.activity = GameActivity.NoAction;
						this.item = 0;
						Asset.click.play(1);
					}
					// Select Item
					else if (touchEvent.y >= 1081) {
						Asset.click.play(1);
						activity = GameActivity.Build;
						item = (int) touchEvent.x / 160 + 1;
					}
				} else if (touchEvent.y >= 851) {
					if (inBounds(touchEvent, 175, 900, 75, 75)) {
						this.activity = GameActivity.Sell;
					}
				}
				// Click Map
				else if (touchEvent.y >= 50) {
					// Touch Map when Build
					if (activity == GameActivity.Build && item != 0) {
						this.listTower.add(new Point(
								50 * (touchEvent.x / 50) + 25,
								50 * (touchEvent.y / 50) + 25));
						Asset.click.play(1);
					}
					// Touch Map when Sell
					else if (activity == GameActivity.Sell) {
						activity = GameActivity.NoAction;
						int x = 50 * (touchEvent.x / 50) + 25;
						int y = 50 * (touchEvent.y / 50) + 25;
						int size = this.listTower.size();
						for (int tower = 0; tower < size; tower++) {
							if (listTower.get(tower).x == x
									&& listTower.get(tower).y == y) {
								listTower.remove(tower);
								activity = GameActivity.Sell;
								Asset.click.play(1);
								break;
							}
						}

					}
				}
			}
		}

	}

	public void updatePause(List<TouchEvent> touchEvents) {

	}

	public void updateGameOver(List<TouchEvent> touchEvents) {

	}

	@Override
	public void present(float deltaTime) {
		// TODO Auto-generated method stub

		// drawWorld(world);
		if (state == GameState.Running) {
			drawRunningUI();
		}
		if (state == GameState.Paused) {
			drawPauseUI();
		}
		if (state == GameState.GameOver) {
			drawGameOverUI();
		}
	}

	public void drawWorld(World world2) {
		// TODO Auto-generated method stub
		Graphics g = game.getGraphics();

	}

	public void drawRunningUI() {
		// TODO Auto-generated method stub

		Graphics g = game.getGraphics();

		g.drawPixmap(Asset.map, 0, 0, 0, 0, 800, 1280);
		g.drawPixmap(Asset.control, 0, 850, 0, 0, 480, 180);
		// Draw Tower
		int len = listTower.size();
		for (int i = 0; i < len; i++) {
			g.drawPixmap(Asset.chosen, listTower.get(i).x - 25,
					listTower.get(i).y - 25, 0, 0, 50, 50);
		}
		// Draw Bar
		if (this.barState == BarState.Basic) {
			g.drawPixmap(Asset.bar, 0, 1030, 800, 0, 800, 250);
		} else if (this.barState == BarState.Advanced) {
			g.drawPixmap(Asset.bar, 0, 1030, 0, 250, 800, 250);
		} else if (this.barState == BarState.Abilities) {
			g.drawPixmap(Asset.bar, 0, 1030, 800, 250, 800, 250);
		}
		// Fill Item is Selected
		if (this.activity == GameActivity.Build) {
			g.drawPixmap(Asset.chosen, (item - 1) * 160, 1080);
		} else if (activity == GameActivity.Sell) {
			g.drawPixmap(Asset.chosen, 193, 865, 0, 0, 176, 150);
		}

	}

	public void drawPauseUI() {
		// TODO Auto-generated method stub
		Graphics g = game.getGraphics();

	}

	public void drawGameOverUI() {
		// TODO Auto-generated method stub
		Graphics g = game.getGraphics();

	}

	public void drawText(Graphics g, String line, int x, int y) {

	}

	@Override
	public void pause() {

	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub

	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}

	public boolean inBounds(TouchEvent event, int x, int y, int width,
			int height) {
		// TODO Auto-generated method stub
		if (event.x > x && event.x < x + width - 1 && event.y > y
				&& event.y < y + height - 1)
			return true;
		else
			return false;
	}
	// private Point toTile(int x, int y){
	// Log.i("parse int", ""+x/50+y/50);
	// return new Point((x/50+1),(int)(y/50));
	//
	// }
	// private drawItem(Point){
	//
	// }

}